package com.foreveross.taskservice.core.domain.statitics.impl;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.foreveross.taskservice.core.domain.statitics.StaticHistoryEntityRepository;
import com.foreveross.taskservice.core.domain.statitics.entity.StaticHistoryTotalEntity;
import com.foreveross.taskservice.infra.ddd.TaskserviceEntityHibernateRepository;
@Repository
public class StaticHistoryEntityRepositoryImpl extends TaskserviceEntityHibernateRepository  implements StaticHistoryEntityRepository {
	@Resource
	private JdbcTemplate jdbctemp;//=(JdbcTemplate) SpringUtil.getBean("jdbcTemplate");
	
	@SuppressWarnings("unchecked")
	@Override
	public List<StaticHistoryTotalEntity> findVoByDate(String startDateStr,String endDateStr) {
		List<StaticHistoryTotalEntity> list=new ArrayList<StaticHistoryTotalEntity>();
		StringBuffer jpql =new StringBuffer("select * from static_history_total where 1=1 ");
		if(startDateStr !=null && !startDateStr.equals("")){
			jpql.append(" and grab_date >= '"+startDateStr+"'");
		}
		if(endDateStr !=null && !endDateStr.equals("")){
			jpql.append(" and grab_date <'"+endDateStr+"'");
		}
		list=(List<StaticHistoryTotalEntity>)jdbctemp.query(jpql.toString(), new ItemMapper());
		return list;
	}
	
	

	@SuppressWarnings("unchecked")
	@Override
	public List<StaticHistoryTotalEntity> findVoByDate(String dateStr) {
		List<StaticHistoryTotalEntity> list=new ArrayList<StaticHistoryTotalEntity>();
		StringBuffer jpql =new StringBuffer("select * from static_history_total where 1=1 ");
		if(dateStr !=null && !dateStr.equals("")){
			jpql.append(" and grab_date='"+dateStr+"'");
		}
		list=(List<StaticHistoryTotalEntity>)jdbctemp.query(jpql.toString(), new ItemMapper());
		return list;
	}
	
	
	@SuppressWarnings({ "rawtypes", "serial" })
	protected class ItemMapper implements RowMapper ,Serializable  {
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			StaticHistoryTotalEntity item=new StaticHistoryTotalEntity();
			item.setId(rs.getString("id"));
			item.setCreateTime(rs.getDate("create_time"));
			item.setGrabDate(rs.getString("grab_date"));
			item.setChannelName(rs.getString("channel_name"));
			item.setChannelId(rs.getLong("channel_id"));
			item.setChannelCode(rs.getString("channel_code"));
			item.setTotalSourceSize(rs.getLong("total_source_size"));
			item.setTotalCountNum(rs.getLong("total_count_num"));
			item.setBusinessFailNum(rs.getLong("business_fail_num"));
			item.setBusinessSuccessNum(rs.getLong("business_success_num"));
			item.setSuspendingNum(rs.getLong("suspending"));
			item.setDoingNum(rs.getLong("doing"));
			item.setWaittingNum(rs.getLong("waitting"));
			item.setTotalSuccessNum(rs.getLong("total_success_num"));
			item.setGrabFailureNum(rs.getLong("grab_failure_num"));
			item.setAnalysisFailureNum(rs.getLong("analysis_failure_num"));
			item.setOtherFailureNum(rs.getLong("other_failure_num"));
			item.setTotalNum(rs.getLong("total"));
			
			return item;
		}
		
	}


}
